package com.dingding.order.core.publish.biz.impl;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.DubboInvokResponseDTO;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.CarShareOrder;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.GetDistanceUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.dto.CalculatDistanceDTO;
import com.dingding.order.core.publish.biz.IRegularRoutePushService;
import com.dingding.order.core.publish.biz.ISaveOrderInfoService;
import com.dingding.order.core.publish.dao.IPublishOrderDao;
import com.dingding.order.dto.publish.ReqReleaseOrderDTO;
import com.dingding.order.dto.publish.ResReleaseOrder;

/**
 * @ClassName: SaveOrderInfoServiceImpl
 * @Description: 保存订单信息Service实现类
 * @author YHQ
 * @date 2016年6月4日 上午11:41:19
 */
@Component("saveOrderInfoService")
public class SaveOrderInfoServiceImpl implements ISaveOrderInfoService {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "publishOrderDao")
	private IPublishOrderDao publishOrderDao;

	@Resource(name = "regularRoutePushService")
	private IRegularRoutePushService regularRoutePushService;

	/**
	 * @Title: saveOrderInfo
	 * @Description: 发布订单 - 保存订单信息
	 * @param reqReleaseOrder 客户端请求参数
	 * @param releaseOrders 发布订单响应Bean
	 * @author YHQ
	 * @return void
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveOrderInfo(ReqReleaseOrderDTO reqReleaseOrder, ResponseEntity<ResReleaseOrder> response) {

		// 用于判断发布订单是否成功 true:成功 false:失败
		Userordert userordert = this.getUserOrderTObj(reqReleaseOrder, response);

		// 判断发布订单成功或失败
		// if (userordert != null) {
		// // 顺路推送
		// regularRoutePushService.regularRoutePush(userordert);
		// }

	}

	/**
	 * 
	 * @Title: getUserOrderTObj
	 * @Description: 获取订单对象
	 * @param reqReleaseOrder 用户下单客户端请求Bean
	 * @param releaseOrders 
	 * @author YHQ
	 * @return userOrdert 下单信息
	 * 
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public Userordert getUserOrderTObj(ReqReleaseOrderDTO reqReleaseOrder, ResponseEntity<ResReleaseOrder> response) {

		Userordert userordert = new Userordert();

		// 订单Id
		userordert.setOid(ToolUtil.getUid());
		
		// 获取下单城市
		CalculatDistanceDTO calculatDistanceDTO = GetDistanceUtil.getSingleDistance(reqReleaseOrder.getStartLongitude(),
				reqReleaseOrder.getStartLatitude(), reqReleaseOrder.getEndLongitude(),
				reqReleaseOrder.getEndLatitude());
		
		// 获取不到下单城市
		if(calculatDistanceDTO == null || ToolUtil.isBlank(calculatDistanceDTO.getCityName())){
			return null;
		}
		// 下单城市
		userordert.setPayCity(calculatDistanceDTO.getCityName());
		// 发布者ID
		userordert.setPlaceOrderUserId(reqReleaseOrder.getPlaceOrderUserId());
		// 下单类型(1:车主下单 2:乘客下单)
		userordert.setOrderType(reqReleaseOrder.getUserType());
		// 出发时间
		userordert.setSetOutTime(DateFormatUtil.yyyy_MM_dd_HHmmss(reqReleaseOrder.getSetOutTime()));
		// 起点
		userordert.setStartAddress(reqReleaseOrder.getStartAddress());
		// 起点经度
		userordert.setStartLongitude(reqReleaseOrder.getStartLongitude());
		// 起点纬度
		userordert.setStartLatitude(reqReleaseOrder.getStartLatitude());
		// 终点
		userordert.setEndAddress(reqReleaseOrder.getEndAddress());
		// 终点经度
		userordert.setEndLongitude(reqReleaseOrder.getEndLongitude());
		// 终点纬度
		userordert.setEndLatitude(reqReleaseOrder.getEndLatitude());
		// 距离
		userordert.setDistance(reqReleaseOrder.getDistance());
		// 红包
		userordert.setRedBag(reqReleaseOrder.getRedBag());
		// 留言
		userordert.setMessage(reqReleaseOrder.getMessage());
		// 人数
		userordert.setNumber(reqReleaseOrder.getNumber());
		// 总金额
		userordert.setTotalMoney(reqReleaseOrder.getOrderCost());
		// 设置叮币默认值
		userordert.setPayMoney(new BigDecimal(0));
		// 设置支付宝支付金额
		userordert.setAlipayMoney(new BigDecimal(0));
		// 是否合乘 isCarShare
		userordert.setIsCarShare(reqReleaseOrder.getIsCarShare());
		// 出发条件 setOutCondition
		userordert.setSetOutCondition(reqReleaseOrder.getSetOutCondition());
		// 当前闲置座位数 freeSeat
		userordert.setFreeSeat(reqReleaseOrder.getFreeSeat());
		// 下单时间
		userordert.setCreateTime(new Date());
		// 订单状态
		userordert.setOrderStatus(1);
		// 是否是长途订单(1:普通 2:长途)
		userordert.setIsLongDistance(reqReleaseOrder.getIsLongDistance());
		// 优惠金额
		userordert.setCouponMoney(reqReleaseOrder.getCouponMoney());
		// 用户优惠券Id
		userordert.setUcId(reqReleaseOrder.getUcid());

		// 判断当前下单用户类型1:车主下单 2:乘客下单，如果为司机则添加主订单Id
		if (reqReleaseOrder.getUserType() == 1) {
			userordert.setIsChildren(1);// 父子订单 1:父订单 2:子订单
			userordert.setFreeSeat(reqReleaseOrder.getNumber());// 座位数
		} else {
			userordert.setIsChildren(2);// 父子订单 1:父订单 2:子订单
			userordert.setFreeSeat(0);// 搭乘人数
		}

		try {
			publishOrderDao.save(userordert);
		} catch (Exception e) {
			log.error("用户下单时出现异常  类：" + this.getClass() + " publishOrder()", e);

			response.setCode(SystemCode.RELEASE_ORDER_FAIL_ERROR);
			response.setErrorMessage(SystemCode.RELEASE_ORDER_FAIL_ERROR_MESSAGE);
			response.setPromptMessage(SystemCode.RELEASE_ORDER_FAIL_ERROR_MESSAGE);
			return null;
		}

		ResReleaseOrder resReleaseOrder = new ResReleaseOrder();

		if (reqReleaseOrder.getUserType() == 1) { // 车主下单
			resReleaseOrder.setParentOrder(userordert.getOid());
		} else { // 乘客下单
			resReleaseOrder.setSubOrder(userordert.getOid());
		}
		// 订单是否为长途订单
		resReleaseOrder.setIsLongDistance(userordert.getIsLongDistance());
		// 订单创建时间
		resReleaseOrder.setOrderCreateTime(userordert.getCreateTime());

		String titleTime = DateFormatUtil.getMMddEHHss(userordert.getSetOutTime());

		StringBuffer sBuffer = new StringBuffer();

		sBuffer.append(titleTime);

		resReleaseOrder.setTitle(sBuffer.toString());

		// 响应参数
		response.setResponseBody(resReleaseOrder);

		response.setCode(SystemCode.SUCCESS);
		response.setErrorMessage(SystemCode.SUCCESS_MESSAGE);
		return userordert;
	}

	/**
	 * @Title: getCarShareOrderObj
	 * @Description: 获取合乘订单对象
	 * @param reqReleaseOrder 发布订单客户端请求Bean
	 * @param releaseOrders 发布订单客户端响应Bean
	 * @author YHQ
	 * @return boolean true:下单成功  false:下单失败
	 */
	@Deprecated
	public Object getCarShareOrderObj(ReqReleaseOrderDTO reqReleaseOrder,
			DubboInvokResponseDTO<ResReleaseOrder> releaseOrders) {

		CarShareOrder carShareOrder = new CarShareOrder();

		// 合乘订单Id
		carShareOrder.setCsId(ToolUtil.getUid());
		// 下单城市
		carShareOrder.setPayCity(reqReleaseOrder.getPayCity());
		// 发布者ID
		carShareOrder.setPlaceOrderUserId(reqReleaseOrder.getPlaceOrderUserId());
		// 下单类型(1:乘客下单 2:车主下单)
		carShareOrder.setOrderType(reqReleaseOrder.getUserType());
		// 出发时间
		carShareOrder.setSetOutTime(DateFormatUtil.yyyy_MM_dd_HHmmss(reqReleaseOrder.getSetOutTime()));
		// 起点
		carShareOrder.setStartAddress(reqReleaseOrder.getStartAddress());
		// 起点经度
		carShareOrder.setStartLongitude(reqReleaseOrder.getStartLongitude());
		// 起点纬度
		carShareOrder.setStartLatitude(reqReleaseOrder.getStartLatitude());
		// 终点
		carShareOrder.setEndAddress(reqReleaseOrder.getEndAddress());
		// 终点经度
		carShareOrder.setEndLongitude(reqReleaseOrder.getEndLongitude());
		// 终点纬度
		carShareOrder.setEndLatitude(reqReleaseOrder.getEndLatitude());
		// 距离
		carShareOrder.setDistance(reqReleaseOrder.getDistance());
		// 红包
		carShareOrder.setRedBag(reqReleaseOrder.getRedBag());
		// 留言
		carShareOrder.setMessage(reqReleaseOrder.getMessage());
		// 人数
		carShareOrder.setNumber(reqReleaseOrder.getNumber());
		// 总金额
		carShareOrder.setTotalMoney(reqReleaseOrder.getOrderCost());
		// 设置叮币默认值
		carShareOrder.setDingBiPayMoney(new BigDecimal(0));
		// 设置支付宝支付金额
		carShareOrder.setRmbPayMoney(new BigDecimal(0));
		// 是否合乘 isCarShare
		carShareOrder.setIsCarShare(reqReleaseOrder.getIsCarShare());
		// 出发条件 setOutCondition
		carShareOrder.setSetOutCondition(reqReleaseOrder.getSetOutCondition());
		// 下单时间
		carShareOrder.setCreateTime(new Date());

		try {
			// carShareOrderDao.save(carShareOrder);
		} catch (Exception e) {
			log.error("乘客下单时出现异常  类：" + this.getClass() + " publishOrder()", e);

			releaseOrders.setCode(SystemCode.RELEASE_ORDER_FAIL_ERROR);
			releaseOrders.setErrorMessage(SystemCode.RELEASE_ORDER_FAIL_ERROR_MESSAGE);
			return null;
		}

		return carShareOrder;
	}

}
